//
//  QFWelcomeViewController.swift
//  sinaWeibo
//
//  Created by apple on 16/2/29.
//  Copyright © 2016年 Qifeng Yan. All rights reserved.
//

import UIKit
import SDWebImage

class QFWelcomeViewController: UIViewController {
    
    // MARK: - 属性
    /// 头像底部约束
    var iconViewBottom: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 加载子控件
        loadNewUI()
        
        // 设置头像
        setIcon()
        
        QFUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("用户信息出错:\(error)")
                return
            }
            
            // 获取到用户信息 加载用户头像 和 昵称
            self.setIcon()
        }
    }
    
    // 设置头像
    private func setIcon() {
        if let avatar_large = QFUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    override func viewDidAppear(animated: Bool) {
        // 头像动画
        moveAnimation()
    }
    
    // MARK: - 动画效果
    private func moveAnimation() {
        // 改变头像底部约束
        iconView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-(UIScreen.mainScreen().bounds.height - 160))
        })
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }){ (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.titleLable.alpha = 1
                    }, completion: { (_) -> Void in
                        // 动画完成后界面跳转到首页
                         AppDelegate.outSwitchRootViewController(true)
                })
        }
    }
    
    // MARK: - 加载UI控件
    private func loadNewUI() {
        // 添加到父控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(titleLable)
        
        // 添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        titleLable.translatesAutoresizingMaskIntoConstraints = false
        
        // VFL 可视化约束
        // H: 水平约束 V: 垂直约束
        // 填充父控件
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView": bkgImageView]))
//        
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView": bkgImageView]))
        
        // 添加头像约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bkgImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 设置头像的大小
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.centerX.equalTo(view)
        }
        
//        // 记录底部约束
//        iconViewBottom = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bkgImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewBottom!)
        
       
        
        // 设置Lable的约束
//        view.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        view.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        titleLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom ).offset(16)
        }
    }
    
    // MARK: - 懒加载子控件
    /// 头像
    private lazy var iconView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        image.layer.cornerRadius = 42.5
        image.layer.masksToBounds = true
        
        return image
    }()
    
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 标题
    private lazy var titleLable: UILabel = {
        let lable = UILabel()
        
        // 设置属性
        lable.text = "欢迎回来"
        lable.textColor = UIColor.blackColor()
        lable.font = UIFont.systemFontOfSize(16)
        lable.sizeToFit()
        lable.alpha = 0
        
        return lable
    }()
    
    
}
